package com.yupi.yuoj.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yuoj.model.entity.Articles;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.model.vo.ArticleVO;
import com.yupi.yuoj.service.ArticlesService;
import com.yupi.yuoj.mapper.ArticlesMapper;
import com.yupi.yuoj.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author 24402
* @description 针对表【articles】的数据库操作Service实现
* @createDate 2025-09-19 08:20:13
*/
@Service
public class ArticlesServiceImpl extends ServiceImpl<ArticlesMapper, Articles>
    implements ArticlesService{

    @Resource
    private UserService userService;

    @Override
    public Page<ArticleVO> getArticleVOPage(long current, long size) {
        // 分页查询文章
        Page<Articles> articlePage = this.page(new Page<>(current, size));
        
        // 获取文章列表
        List<Articles> articleList = articlePage.getRecords();
        if (articleList.isEmpty()) {
            return new Page<>(current, size, 0);
        }
        
        // 提取所有用户ID
        Set<Long> userIdSet = articleList.stream()
                .map(Articles::getUser_id)
                .collect(Collectors.toSet());
        
        // 批量查询用户信息
        Map<Long, User> userMap = userService.listByIds(userIdSet).stream()
                .collect(Collectors.toMap(User::getId, user -> user));
        
        // 转换为VO对象
        List<ArticleVO> articleVOList = articleList.stream().map(article -> {
            ArticleVO articleVO = ArticleVO.objToVo(article);
            User user = userMap.get(article.getUser_id());
            if (user != null) {
                articleVO.setNickname(user.getNickname());
            }
            return articleVO;
        }).collect(Collectors.toList());
        
        // 构建返回的分页对象
        Page<ArticleVO> articleVOPage = new Page<>(current, size, articlePage.getTotal());
        articleVOPage.setRecords(articleVOList);
        
        return articleVOPage;
    }

    @Override
    public Page<ArticleVO> getArticleVOPage(long current, long size, Long categoryId) {
        // 构建查询条件
        Page<Articles> articlePage;
        if (categoryId != null && categoryId > 0) {
            articlePage = this.lambdaQuery()
                    .eq(Articles::getCategory_id, categoryId)
                    .eq(Articles::getStatus, 1) // 只显示已发布的文章
                    .orderByDesc(Articles::getCreated_at)
                    .page(new Page<>(current, size));
        } else {
            articlePage = this.lambdaQuery()
                    .eq(Articles::getStatus, 1) // 只显示已发布的文章
                    .orderByDesc(Articles::getCreated_at)
                    .page(new Page<>(current, size));
        }
        
        // 获取文章列表
        List<Articles> articleList = articlePage.getRecords();
        if (articleList.isEmpty()) {
            return new Page<>(current, size, 0);
        }
        
        // 提取所有用户ID
        Set<Long> userIdSet = articleList.stream()
                .map(Articles::getUser_id)
                .collect(Collectors.toSet());
        
        // 批量查询用户信息
        Map<Long, User> userMap = userService.listByIds(userIdSet).stream()
                .collect(Collectors.toMap(User::getId, user -> user));
        
        // 转换为VO对象
        List<ArticleVO> articleVOList = articleList.stream().map(article -> {
            ArticleVO articleVO = ArticleVO.objToVo(article);
            User user = userMap.get(article.getUser_id());
            if (user != null) {
                articleVO.setNickname(user.getNickname());
            }
            return articleVO;
        }).collect(Collectors.toList());
        
        // 构建返回的分页对象
        Page<ArticleVO> articleVOPage = new Page<>(current, size, articlePage.getTotal());
        articleVOPage.setRecords(articleVOList);
        
        return articleVOPage;
    }

}




